package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.PageDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.query.EmployeeQuery;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.mapper.EmployeeMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.Objects;

/**
* @author 马毅爽
* @description 针对表【employee(员工信息)】的数据库操作Service实现
* @createDate 2023-11-02 22:38:46
*/
@Service
@Slf4j
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>
    implements EmployeeService{

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        Employee employee = lambdaQuery()
                .eq(Employee::getUsername,username)
                .list().get(0);

        if (employee == null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())){
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE){
            //账户被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        return employee;
    }

    @Override
    public void save(EmployeeDTO employeeDTO) {
        Employee employee = BeanUtil.copyProperties(employeeDTO,Employee.class);

        employee.setStatus(StatusConstant.ENABLE);

        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));

        save(employee);
    }

    @Override
    public PageDTO<Employee> queryPage(EmployeeQuery employeeQuery) {
        String name = employeeQuery.getName();

        Page<Employee> page = employeeQuery.sortByUpdateTime();
        Page<Employee> p = lambdaQuery()
                .like(name != null,Employee::getName,name)
                .page(page);
        return PageDTO.of(p);
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Employee employee = Employee.builder()
                .status(status)
                .id(id)
                .build();
        updateById(employee);
    }

    @Override
    public Employee queryById(Long id) {
        Employee employee = getById(id);
        employee.setPassword("****");
        return employee;
    }

    @Override
    public void updateInfo(EmployeeDTO employeeDTO) {
        Employee employee = BeanUtil.copyProperties(employeeDTO,Employee.class);
        updateById(employee);

    }

    @Override
    public void updatePassword(PasswordEditDTO passwordEditDTO) {
        String old_password = DigestUtils.md5DigestAsHex(passwordEditDTO.getOldPassword().getBytes());
        String new_password = DigestUtils.md5DigestAsHex(passwordEditDTO.getNewPassword().getBytes());
        Employee employee = lambdaQuery()
                .eq(Employee::getId,passwordEditDTO.getEmpId())
                .list().get(0);

        if (!Objects.equals(employee.getPassword(), old_password)){
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        employee.setPassword(new_password);
        updateById(employee);
    }
}




